﻿using System;
using Tiny.Imaging.Filter.PixelLibrary;

namespace Tiny.Imaging.Filter.PointFilter
{
    /// <summary>
    /// Usage Reference:
    /// It is like normal blend mode with a white layer on top.
    /// 
    /// To have similar result as Brightness in Photoshop, use overlay with white layer by adjusting opacity
    /// </summary>
    public class BrightnessFilter : BaseFilter.BaseFilter
    {
        private int[] _mapping;

        public BrightnessFilter(float factor, Func<int, int, int> blend = null, int opacity = 100)
            : base(blend, opacity)
        {
            PrePopulate((int)(factor));
        }

        public override int Process(Rgba rgba)
        {
            rgba.R = _mapping[rgba.R];
            rgba.G = _mapping[rgba.G];
            rgba.B = _mapping[rgba.B];
            return rgba.ToInteger();
        }

        private void PrePopulate(float factor)
        {
            _mapping = new int[256];

            for (int i = 0; i < 256; ++i)
            {
                _mapping[i] = Blend(i, MathLibrary.MathLibrary.Clamp((int)(i + factor)));
            }
        }
    }
}